package com.league.zjxn.main.mine

import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.league.zjxn.R
import com.league.zjxn.jetpack.vmodel.AccessViewModel
import com.league.zjxn.main.popup.LoadingDialog
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_setting_account.*

class SettingAccountActivity : OwnerViewModeActivity<AccessViewModel>() {

    companion object {
        private const val KEY_ACCOUNT_ACTION = "KEY_ACCOUNT_ACTION"
        fun openPhone() {
            val bundle = Bundle().apply {
                putString(KEY_ACCOUNT_ACTION, "phone")
            }
            ActivityUtils.startActivity(bundle, SettingAccountActivity::class.java)
        }

        fun openPwd() {
            val bundle = Bundle().apply {
                putString(KEY_ACCOUNT_ACTION, "password")
            }
            ActivityUtils.startActivity(bundle, SettingAccountActivity::class.java)
        }
    }

    private val mAction: String by lazy {
        intent?.extras?.getString(KEY_ACCOUNT_ACTION) ?: "phone"
    }

    override fun viewModel(): AccessViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_setting_account

    override fun stateBar(): StatusBar {
        return StatusBar(view = flSettingAccountBar)
    }

    override fun initView() {
        if (mAction == "password") {
            supportFragmentManager(AccountPassFragment.newInstance(), (false))
        } else {
            supportFragmentManager(AccountPhoneFragment.newInstance(), (false))
        }
    }

    override fun initViewMode() {
        viewModel.userInfoLiveData.observe(this as LifecycleOwner, { response ->
            responseLoadingDialog(response)
            if (response.status != Status.SUCCESS)
                return@observe
            toast(response.data?.msg)
            finish()
        })

        viewModel.sendSmsCodeLiveData.observe(this as LifecycleOwner, { response ->
            responseLoadingDialog(response)
            if (response.status != Status.SUCCESS)
                return@observe
            supportFragmentManager(AccountCodeFragment.newInstance())
        })
    }

    private fun responseLoadingDialog(response: HttpResponseBody<*>) {
        when (response.status) {
            Status.START -> LoadingDialog.show(
                this as Context, resId = R.layout.dialog_load_style_1
            )
            Status.FAILURE -> {
                LoadingDialog.dismiss()
                toast(response.error?.msg)
            }
            Status.SUCCESS -> {
                LoadingDialog.dismiss()
            }
        }
    }

    fun onClickAccountPhone(view: View? = null) {
        viewModel.userSendSmsCodeLogin()
    }

    fun onClickAccountCode(view: View? = null) {
        viewModel.userSettingPhone()
    }

    fun onClickAccountPass(view: View? = null) {
        viewModel.userSettingPwd()
    }

    private fun supportFragmentManager(fragment: Fragment, isToBackStack: Boolean = true) {
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        if (isToBackStack) {
            transaction.setCustomAnimations(
                R.animator.slide_right_in, R.animator.slide_left_out,
                R.animator.slide_left_in, R.animator.slide_right_out
            )
            transaction.replace(R.id.flSettingAccountBody, fragment)
            transaction.addToBackStack(null)
        } else {
            transaction.add(R.id.flSettingAccountBody, fragment)
        }
        transaction.commit()
    }
}